<template>
<div>
  <home-header></home-header>
  <home-swiper :list="swiperlist"></home-swiper>
  <home-icon :iconlist="iconlist"></home-icon>
  <home-recommed :recommed="recommedlist"></home-recommed>
  <home-week :week="weeklist"></home-week>
</div>
</template>

<script>
import HomeHeader from './components/Header'
import HomeSwiper from './components/Swiper'
import HomeIcon from './components/Icon'
import HomeRecommed from './components/Recommed'
import HomeWeek from './components/Weekend'
import { mapState } from 'vuex'
// eslint-disable-next-line no-unused-vars
import axios from 'axios'
export default {
  name: 'Home',
  components: {
    HomeHeader,
    HomeSwiper,
    HomeIcon,
    HomeRecommed,
    HomeWeek
  },
  data () {
    return {
      lastCity: '',
      swiperlist: [],
      iconlist: [],
      recommedlist: [],
      weeklist: []
    }
  },
  computed: {
    ...mapState(['city'])
  },
  methods: {
    // getHomeInfo读取json数据
    getHomeInfo () {
      axios.get('/api/index.json?city=' + this.city)
        .then(this.getHomeInfoSucc)
    },
    getHomeInfoSucc (res) {
      res = res.data
      // 后端正确返回结果res.ret，且res里面存在res.data内容
      if (res.ret && res.data) {
        const data = res.data
        this.swiperlist = data.swiperList
        this.iconlist = data.iconList
        this.recommedlist = data.recommedList
        this.weeklist = data.weekList
      }
    }
  },
  mounted () {
    this.getHomeInfo()
    // console.log('mounted')
    this.lastCity = this.city
  },
  // keep-alive自带的生命周期函数，每次页面加载时都会执行一次，为实现切换城市时首页内容跟着变化
  activated () {
    // console.log('activated')
    if (this.lastCity !== this.city) {
      // 重新调用ajax请求
      this.lastCity = this.city
      this.getHomeInfo()
    }
  }
}
</script>

<style scoped>
</style>
